<?php
App::uses('AppController', 'Controller');
App::uses('Sanitize', 'Utility');
/**
 * Colonnes Controller
 *
 * @property Colonne $Colonnes
 */
class RupturesController extends AppController {

	//var $components = array('Maconnexion', 'Session');
	public $helpers = array('Date', 'Datefr', 'Moninput', 'Session_gpp');
	
	public $paginate = array(
        'limit' => 25,
        'order' => array(
            'Colonne.topPk' => 'desc','Colonne.id' => 'asc'
        )
    );


	public function view($id, $version) {
		$id = Sanitize::paranoid($id);
		$version = Sanitize::paranoid($version);
		$this->Rupture->id = $id;
		if (!$this->Rupture->exists()) {
			throw new NotFoundException(__('Invalid table'));
		}
		$versionencours = $this->Rupture->Table->Version->find('first',array('conditions' => array('Version.id' => $version),'recursive'=>-1));
		
		$unrupture = $this->Rupture->find('first',array('conditions' => array('Rupture.id' => $id),'recursive'=>-1));
		
		$this->Rupture->Table->recursive = 1;
		$satable=$this->Rupture->Table->read(null, $unrupture['Rupture']['table_id']);

		//$satable = $this->Rupture->Table->find('first',array('conditions' => array('Table.id' => $unrupture['Rupture']['table_id']),'recursive'=>-1));
		$saversion = $this->Rupture->Table->Version->find('first',array('conditions' => array('Version.id' => $satable['Table']['version_id']),'recursive'=>-1));
		$sonapp = $this->Rupture->Table->Version->Application->find('first',array('conditions' => array('Application.id' => $saversion['Version']['application_id']),'recursive'=>-1));
		//$this->set('satable', $satable);

		$this->Rupture->recursive = -1;
		$data=$this->Rupture->read(null, $id);
		//$data = $this->Table->find('all', array( 'conditions' => array('Table.id =' => $id),'order'=>array('topPk desc, id asc'), 'recursive'=>1 ));
		
		$colonnes = $this->Rupture->Table->Colonne->find('all', array( 'conditions' => array('Colonne.table_id =' => $satable['Table']['id']),'order'=>array('topPk desc, id asc'), 'recursive'=>-1 ));
		//$sapk = $this->Table->Colonne->find('all', array('conditions' => array('Colonne.table_id =' => $id, 'Colonne.topPk =' => '1'),'recursive'=>-1 ));
		//$data['Colonne']=$colonnes;
		$i=0;
		foreach ($colonnes as $colonne): 
			$colonne=$colonne['Colonne'];
			//$datatable['Colonne'][$i]=$colonne;
			$pgtypecol_name = $this->Rupture->Table->Colonne->Pgtypecol->find('first',array('conditions' => array('id' => $colonne['pgtypecol_id']),'recursive'=>-1));
			$satable['Colonne'][$i]['pgtypecol_name'] = $pgtypecol_name['Pgtypecol']['name'];
			$i=$i+1;
		endforeach;
		$this->set('saversion', $saversion);
		$this->set('sonapp', $sonapp);
		$this->set('satable', $satable);
		$this->set('rupture', $data);
		$this->set('rupture_id', $id);
		$this->set('versionencours', $versionencours);
	}

/**
 * add method
 *
 * @return void
 */
	public function add($idtable, $idversion) {
		//debug($this->request->data);
		if ($this->request->is('post')) {
			$this->request->data = Sanitize::clean($this->request->data, array('encode' => false, 'unicode'=> true));
			$this->Rupture->create();
			if ($this->Rupture->save($this->request->data)) {
				$this->Session->setFlash(__('The rupture has been saved'),'default',array("class"=>'success'));
				$this->redirect(array('controller'=>'tables', 'action' => 'view',$idtable, $idversion));
			} else {
				$this->Session->setFlash(__('The rupture could not be saved. Please, try again.'),'default',array("class"=>'error'));	
			}
		}

		//$this->request->data['lstfonctions']=$this->Rupture->Table->Version->Application->Fonction->find('list');
		//$this->request->data['Rupture']['name']='RLEB';

		$this->set('idtable',$idtable);
		$this->set('idversion',$idversion);
		//$this->set(compact('pgtypecols'));
	}
}